<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" :labelWidth="'90'" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable ref="table" :columns="columns" :fetchapi="fetchApi" :params="fetchParams" :datakey="'rows'" :onColumnsChange="columns => (this.columns = columns)">
      <template slot="controls" slot-scope="props">
        <el-button size="small">导出</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" title="参与活动车辆明细" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" destroyOnClose>
      <detailActivityVehicle :activityCode="activityCode" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import detailActivityVehicle from './detailActivityVehicle';
import { getPage } from '@/api/repair/activitiesManage/activitiesQuery';

export default {
  name: 'activityVehicle',
  components: {
    detailActivityVehicle
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: getPage,
      visible: false,
      fetchParams: { xhrAbort: true },
      columns: this.createTableColumns(),
      activityCode: ''
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.isEdit(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '活动编号',
          dataIndex: 'activity_code',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动名称',
          dataIndex: 'activity_name',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开始日期',
          dataIndex: 'begin_date',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '结束日期',
          dataIndex: 'end_date',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '活动状态',
          dataIndex: 'is_activity_status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8115'),
          render: props => {
            return <span>{this.createDictText(props.row.is_activity_status, '8115')}</span>;
          }
        },
        {
          title: '发布日期',
          dataIndex: 'release_date',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '工时费',
          dataIndex: 'labour_amount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '完成车辆数',
          dataIndex: 'achieve_car_sum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '维修材料费',
          dataIndex: 'repair_part_amount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动总金额',
          dataIndex: 'activity_amount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'down_tag',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.down_tag, '1045')}</span>;
          }
        },
        {
          title: '责任车辆数',
          dataIndex: 'duty_car_sum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '责任完成率',
          dataIndex: 'duty_rate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '总活动车辆数',
          dataIndex: 'all_car_sum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '总完成率',
          dataIndex: 'achieve_rate',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '活动编号',
          fieldName: 'activityCode',
          placeholder: '活动编号...'
        },
        {
          type: 'INPUT',
          label: '活动名称',
          fieldName: 'activityName',
          placeholder: '活动名称...'
        },
        {
          type: 'SELECT',
          label: '活动状态',
          fieldName: 'isActivityStatus',
          placeholder: '活动状态',
          itemList: this.createDictList('8115')
        },
        {
          type: 'RANGE_DATE',
          label: '活动日期区间',
          fieldName: 'endDateStart|endDateEnd',
          placeholder: '请输入...'
        }
      ];
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    isEdit(row) {
      this.visible = true;
      this.type = 'edit';
      this.activityCode = row.activity_code;
    },
    closeHandler(val) {
      this.visible = val;
    },
    dictText(val, code) {
      let res = '';
      if (!val) return res;
      if (val == 10041001) {
        res = '过期';
      }
      if (val == 10041002) {
        res = '不过期';
      }
      return res;
    }
  }
};
</script>

<style lang="less" scoped></style>
